package com.ssms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ssms.common.R;
import com.ssms.domain.*;
import com.ssms.mapper.CourseMapper;
import com.ssms.mapper.CourseTeacherMapper;
import com.ssms.mapper.ExamScoreMapper;
import com.ssms.service.StudentService;
import com.ssms.service.TeacherService;
import com.ssms.utils.JwtUtils;
import com.ssms.vo.CourseInfoVo;
import com.ssms.vo.GradeInfoVo;
import com.ssms.vo.StudentInfoVo;
import com.ssms.vo.paras.ChangeStudentInfoPara;
import com.ssms.vo.paras.RegisterPara;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class TeacherController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private ExamScoreMapper examScoreMapper;
    @Autowired
    private CourseTeacherMapper courseTeacherMapper;

    @Autowired
    private TeacherService teacherService;
    @PostMapping("/changeStuInfo")
    public R<Object> changeStuInfo(@RequestBody ChangeStudentInfoPara para){
        LambdaQueryWrapper<Student> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(Student::getStudentNum,para.getId());
        Student st = studentService.getOne(queryWrapper2);
        st.setStudentName(para.getName());
        st.setStudentClass(para.getTclass());
        studentService.saveOrUpdate(st);
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_name",para.getCourse());
        Course course = courseMapper.selectOne(queryWrapper);
        int courseId = course.getCourseId();
        //更新分数
//        QueryWrapper<ExamScore> queryWrapper1 = new QueryWrapper<>();
//        queryWrapper1.eq("c_id",courseId).eq("s_id",st.getStudentId());
        LambdaQueryWrapper<ExamScore> queryWrapper3 = new LambdaQueryWrapper<>();
        queryWrapper3.eq(ExamScore::getSId,st.getStudentId());
        queryWrapper3.eq(ExamScore::getCId,courseId);
        ExamScore examScore = examScoreMapper.selectOne(queryWrapper3);
        examScore.setPositiveScore(Double.parseDouble(para.getGrade()));
        examScoreMapper.update(examScore,queryWrapper3);

        return R.success("修改成功");
    }
    @GetMapping("/getTeacherInfo")
    public R<Object> getTeacherInfo(@RequestHeader String token) throws Exception{

        Claims claims = JwtUtils.getClaims(token);
        int id = (int)claims.get("id");
        String name = (String) claims.get("name");
        String role = (String) claims.get("role");
        String num = (String) claims.get("number");
        StudentInfoVo st = new StudentInfoVo();
        st.setName(name);

        return R.success(st,"获取成功");
    }

    @GetMapping("/getSourceData")
    public R<Object> getSourceData(@RequestHeader String token) throws Exception{

        Claims claims = JwtUtils.getClaims(token);
        int id = (int)claims.get("id");
        String name = (String) claims.get("name");
        String role = (String) claims.get("role");
        String num = (String) claims.get("number");
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getTId,id);
        CourseTeacher courseTeacher = courseTeacherMapper.selectOne(queryWrapper);
        LambdaQueryWrapper<Course> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Course::getCourseId,courseTeacher.getCId());
        Course course = courseMapper.selectOne(queryWrapper1);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        courseInfoVo.setSourceName(course.getCourseName());
        courseInfoVo.setPosition(course.getPosition());
        courseInfoVo.setEndTime(course.getEndTime());
        courseInfoVo.setStartTime(course.getStartTime());
        courseInfoVo.setTestTime(course.getTestTime());
        courseInfoVo.setStuNumber(course.getStuNum());

        return R.success(courseInfoVo,"获取成功");
    }

    @PostMapping("/changeSourceInfo")
    public R<Object> changeSourceInfo(@RequestHeader String token,@RequestBody CourseInfoVo courseInfoVo) throws Exception{

        Claims claims = JwtUtils.getClaims(token);
        int id = (int)claims.get("id");
        String name = (String) claims.get("name");
        String role = (String) claims.get("role");
        String num = (String) claims.get("number");
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getTId,id);
        CourseTeacher courseTeacher = courseTeacherMapper.selectOne(queryWrapper);
        LambdaQueryWrapper<Course> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Course::getCourseId,courseTeacher.getCId());
        Course course = courseMapper.selectOne(queryWrapper1);
        course.setCourseName(courseInfoVo.getSourceName());
        course.setTestTime(courseInfoVo.getTestTime());
        course.setPosition(courseInfoVo.getPosition());
        course.setEndTime(courseInfoVo.getEndTime());
        course.setStartTime(courseInfoVo.getStartTime());
        course.setStuNum(courseInfoVo.getStuNumber());
        courseMapper.updateById(course);

        return R.success("修改成功");
    }

    @GetMapping("/deleteStuInfo")
    public R<Object> deleteStuInfo(@RequestHeader String token,String id) throws Exception{
        studentService.removeById(id);
        return R.success("删除成功");
    }
    @GetMapping("/getStuGrade")
    public R<Object> getStuGrade(@RequestHeader String token) throws Exception{

        Claims claims = JwtUtils.getClaims(token);
        //获取班主任id
        int id = (int)claims.get("id");
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getCounsellorId,id);
        //拿到班主任对应的学生列表
        List<Student> studentList = studentService.list(queryWrapper);
        List<GradeInfoVo> gradeInfoVoList = new ArrayList<>();
        //循环查学生的成绩
        for (Student student : studentList) {
            LambdaQueryWrapper<ExamScore> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(ExamScore::getSId,student.getStudentId());
            List<ExamScore> examScoreList = examScoreMapper.selectList(queryWrapper1);
            for (ExamScore examScore : examScoreList) {
                LambdaQueryWrapper<Course> courseLambdaQueryWrapper = new LambdaQueryWrapper<>();
                courseLambdaQueryWrapper.eq(Course::getCourseId,examScore.getCId());
                Course course = courseMapper.selectOne(courseLambdaQueryWrapper);
                GradeInfoVo gradeInfoVo = new GradeInfoVo();
                gradeInfoVo.setId(student.getStudentNum());
                gradeInfoVo.setTclass(student.getStudentClass());
                gradeInfoVo.setName(student.getStudentName());
                gradeInfoVo.setGrade(examScore.getPositiveScore());
                gradeInfoVo.setCourse(course.getCourseName());
                gradeInfoVoList.add(gradeInfoVo);
            }
        }

        return R.success(gradeInfoVoList,"查询成功");
    }

}
